package com.czl.elm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czl.elm.dao.OrderMapper;
import com.czl.elm.dao.ShopMapper;
import com.czl.elm.dao.impl.FoodMapperImpl;
import com.czl.elm.dao.impl.UsersMapperImpl;
import com.czl.elm.domain.Order;
import com.czl.elm.domain.RedParper;
import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.service.IOrderService;
import com.czl.elm.util.ActionResult;
import com.czl.elm.util.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuoleiChen
 * @since 2021-06-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private FoodMapperImpl foodMapper;
    @Autowired
    private UsersMapperImpl usersMapper;

    @Override
    public ActionResult createOrder(Map<String, Object> msg) {
        msg=(Map<String, Object>) msg.get("msg");
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        String header="order_";
        Order order=new Order();
        order.setAddressId((String)msg.get("addressId"));
        order.setBuyWay((String)msg.get("buyWay"));
        order.setDistributionCost((Integer)msg.get("distributionCost"));
        order.setEnable(1);
        order.setFoodPack((Integer)msg.get("foodPack"));
        order.setPreferential(msg.containsKey("preferential")? JSON.toJSONString(msg.get("preferential")):null);
        order.setOrderId(header+phoneName+System.currentTimeMillis());
        order.setShopCart(msg.containsKey("shopCart")? JSON.toJSONString(msg.get("shopCart")):null);
        order.setSendTime(msg.containsKey("sendTime")?(String)msg.get("sendTime"):null);
        order.setSumReduction((Integer)msg.getOrDefault("sumReduction",null));
        order.setSumCost((Integer)msg.getOrDefault("sumCost",null));
        order.setTag((Integer)msg.getOrDefault("tag",null));
        order.setShopId((Integer)msg.getOrDefault("shopId",null));
        order.setGetTime(LocalDateTime.now());
        order.setKnight("骑手");
        order.setPhoneName(phoneName);

        Integer res=orderMapper.createOrder(order);

        if(res>0)return ActionResult.success("生成成功",order.getOrderId());
        else return ActionResult.error("生成失败");
    }


    @Override
    public ActionResult getOrder(String orderId) {
        Order order=orderMapper.getOrderById(orderId);
        Map<String, Object> res = JSONObject.parseObject(JSONObject.toJSONString(order));
        Integer shopId=order.getShopId();
        Map<String,Object> shopBase= shopMapper.selectShopBasic(shopId);
        res.put("shopBase",shopBase);
        String shopCart=order.getShopCart();
        Map<String, Object> shopCartMap = JSONObject.parseObject(shopCart);
        res.put("shopCart",shopCartMap);
        List<String> foodIdList=new ArrayList<>();
        for(String foodId:shopCartMap.keySet()){
            foodIdList.add(foodId);
        }
        List<Map> foodDetails=foodMapper.getFoodByFoodIdList(foodIdList);
        res.put("foodDetails",foodDetails);
        Map<String, Object> preferential = JSONObject.parseObject(order.getPreferential());
        res.put("preferential",preferential);
        res.put("addressDetails",usersMapper.getUsersAddressById(order.getPhoneName(),order.getAddressId()));
        return ActionResult.success("获取成功",res);
    }

    public Map<String,Object> getOrder_base(String orderId) {
        Map<String,Object> res=orderMapper.getOrderByIdBasic(orderId);

        Integer shopId=(Integer)res.get("shopId");
        Map<String,Object> shopBase= shopMapper.selectShopBasic(shopId);
        res.put("shopBase",shopBase);
        String shopCart=(String)res.get("shopCart");
        Map<String, Object> shopCartMap = JSONObject.parseObject(shopCart);
        List<String> foodIdList=new ArrayList<>();
        for(String foodId:shopCartMap.keySet()){
            foodIdList.add(foodId);
        }
        List<Map> foodDetails=foodMapper.getFoodByFoodIdList(foodIdList);
        res.put("foodDetails",foodDetails);


        return res;
    }
    @Override
    public ActionResult getOrderList(Integer currentPage, Integer size) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        Page<String> page=new Page<>(currentPage,size);
        IPage<String> res_page=orderMapper.getOrderList(phoneName,page);
        List<Map<String,Object>> res=new ArrayList<>();
        for(String orderId:res_page.getRecords()){
            res.add(getOrder_base(orderId));
        }
        RespPageBean result=new RespPageBean(res_page.getTotal(),res);
        return ActionResult.success("获取成功",result);
    }

    @Override
    public ActionResult getOrderBase(String orderId) {
        return ActionResult.success("获取成功",getOrder_base(orderId));
    }
}
